package club.yuyang.campus2mall.service.impl;

import club.yuyang.campus2mall.dto.AdMenuDto;
import club.yuyang.campus2mall.entity.AdMenu;
import club.yuyang.campus2mall.mapper.AdMenuMapper;
import club.yuyang.campus2mall.service.AdMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*
 *@ClassName AdMenuServiceImpl
 *@Description TODO
 *@Author yuyang
 *@Date 2020/11/30 10:18
 */
@Service
public class AdMenuServiceImpl extends ServiceImpl<AdMenuMapper, AdMenu> implements AdMenuService {
    @Resource
    private AdMenuMapper adMenuMapper;

    @Override
    public List<AdMenuDto> queryList() {
        //查询所有一级菜单，并按“px”字段排序
        List<AdMenu> list = adMenuMapper.selectList(new QueryWrapper<AdMenu>().eq("level",1).orderByAsc("px"));
        //用来存储AdmenuDto类型的数据对象集合
        List<AdMenuDto> adMenuDtoList = new ArrayList<>();
        for (AdMenu adMenu : list){
            AdMenuDto adMenuDto = new AdMenuDto();
            //将adMenu中的属性赋值到adMenuDto中
            BeanUtils.copyProperties(adMenu,adMenuDto);
           List<AdMenu> adMenus = adMenuMapper.selectList(new QueryWrapper<AdMenu>().eq("parent_id",adMenu.getMenuId()).orderByAsc("px"));
           adMenuDto.setChildrenList(adMenus);
           adMenuDtoList.add(adMenuDto);
        }
        return adMenuDtoList;
    }
}
